package com.ipodget

import org.apache.lucene.search.Query
import org.apache.lucene.store.FSDirectory
import org.apache.lucene.search.IndexSearcher
import org.apache.lucene.queryParser.QueryParser
import org.apache.lucene.analysis.standard.StandardAnalyzer

public class IpodIndexSearcher{
	File indexRoot
	
	IpodIndexSearcher(File indexRoot){
		this.indexRoot = indexRoot
	}
	
	def search = { searchQuery, logger ->
		QueryParser queryParser = new QueryParser("all", new StandardAnalyzer())
		Query query = queryParser.parse(searchQuery)
		def indexPath = FSDirectory.getDirectory(indexRoot)
		def searcher = new IndexSearcher(indexPath)
		logger?.call "Searching index..."
		def hits = searcher.search(query)
		def results = []
		def hitCount = hits.length()
		logger?.call "Found $hitCount hits"
		for ( ix in 0 ..< hitCount ) {
			results << hits.doc(ix)
		}
		return results
	}
}
